package name.gauravmadan.mdg.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import name.gauravmadan.mdg.pim.Application;
import name.gauravmadan.mdg.service.ModelDataService;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class ApplicationFormController extends SimpleFormController{
	private ModelDataService service;
	
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
			Object command, BindException errors)
			throws ServletException, IOException
	{
		// cast the bean
		Application application = (Application) command;
		if (application != null) {
			if (application.getId() == null) {
				service.createApplication(application);
			}
			else {
				service.updateApplication(application);
			}
		}
		
		return new ModelAndView("redirect:/application-list.html?method=list");
	}
	
	// Getters and Setters
	public ModelDataService getService() {
		return service;
	}
	public void setService(ModelDataService service) {
		this.service = service;
	}
}
